package ui.activity.profit;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.listener.OnDateSetListener;

import net.ck.hzq.R;
import net.ck.hzq.TobuyApplication;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import base.BaseAct;
import base.BaseFra;
import butterknife.BindView;
import event.NetErrorEvent;
import event.RankingEvent;
import ui.adapter.FragAdp;
import ui.fragment.Ranking2Fra;
import ui.fragment.RankingFra;
import util.AlertUtils;
import util.SysUtils;
import widget.tab.TabPageIndicator;


/**
 * 收藏
 * Created by chenjian on 18/2/22.
 */

public class RankingAct extends BaseAct {


    @BindView(R.id.my_tabs)
    TabPageIndicator myTab;
    @BindView(R.id.my_vp)
    ViewPager vp;
    @BindView(R.id.needsx)
    TextView needsx;


    public List<BaseFra> fragments;
    List<String> titles = new ArrayList<>();

    long selectTime;

    @Override
    protected void setRoot() {
//        setTitleBar("#0099E6");
        setContentView(R.layout.activity_ranking);

    }

    @Override
    protected void setup() {
        selectTime = System.currentTimeMillis();
        setToolBar(getResources().getString(R.string.lang_ranking_title), "");
        fragments = new ArrayList<>();
        titles.add(getResources().getString(R.string.lang_ranking_lab1));
        titles.add(getResources().getString(R.string.lang_ranking_lab2));
//        for (int i = 0; i < titles.size(); i++) {
//            RankingFra fra = new RankingFra();
//            Bundle bundle = new Bundle();
//            bundle.putInt(Constants.KEY, i);
//            fra.setArguments(bundle);
//            fragments.add(fra);
//        }
        fragments.add(new RankingFra());
        fragments.add(new Ranking2Fra());
        vp.setPageMargin(SysUtils.dip2px(this, 0f));
        vp.setOffscreenPageLimit(titles.size());
        vp.setAdapter(new FragAdp(this.getSupportFragmentManager(), titles, fragments));
        myTab.setViewPager(vp);
        setTabPagerIndicator();
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        try {
            String str = SysUtils.sf2.format(new Date(System.currentTimeMillis()))
                    .substring(5, SysUtils.sf2.format(new Date(System.currentTimeMillis())).length())
                    + getResources().getString(R.string.lang_mouth);
            needsx.setText(str);
        } catch (Exception e) {

        }
        RxView.clicks(needsx)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> {
                    TimePickerDialog timePickerDialog = AlertUtils.timePicker3(RankingAct.this, 365 * 5, 0, selectTime, new OnDateSetListener() {
                        @Override
                        public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
                            selectTime = millseconds;
                            String str = SysUtils.sf2.format(new Date(millseconds))
                                    .substring(5, SysUtils.sf2.format(new Date(millseconds)).length())
                                    + getResources().getString(R.string.lang_mouth);
                            needsx.setText(str);
                            EventBus.getDefault().post(new RankingEvent(SysUtils.sf2.format(new Date(millseconds))));
                        }
                    });
                    timePickerDialog.show(RankingAct.this.getSupportFragmentManager(), "all");
                });
    }


    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }


    @Override
    protected void onStop() {

        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    /**
     * 设置标题栏
     */

    private void setTabPagerIndicator() {
        myTab.setIndicatorMode(TabPageIndicator.IndicatorMode.MODE_NOWEIGHT_EXPAND_SAME);// 设置模式，一定要先设置模式
//        myTab.setDividerColor(Color.parseColor("#0099E6"));// 设置分割线的颜色
        myTab.setDividerPadding(SysUtils.dip2px(TobuyApplication.getInstance(), 0));
        myTab.setIndicatorColor(Color.parseColor("#0099E6"));// 设置底部导航线的颜色
        myTab.setTextColorSelected(Color.parseColor("#0099E6"));// 设置tab标题选中的颜色
        myTab.setTextColor(Color.parseColor("#333333"));// 设置tab标题未被选中的颜色
        myTab.setTextSize((int) SysUtils.sp2px(TobuyApplication.getInstance(), 14));// 设置字体大小
    }

}
